package com.p2p.backend.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.p2p.backend.dao.AccountInternalDao;
import com.p2p.backend.service.AccountInternalService;
import com.p2p.trans.client.facade.AccountQueryFacade;
import com.p2p.trans.client.model.AccountVO;
import com.p2p.trans.client.result.AccountQueryResult;

/**
 * leiwen@yiqihao.com
 */
@Service("accountInternalService")
public class AccountInternalServiceImpl implements AccountInternalService {

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource
	private AccountQueryFacade accountQueryFacade;
	
	@Resource
	private AccountInternalDao accountInternalDao;
	
	@Override
	public List<Map<String, Object>> list() {
		List<Map<String,Object>> list=accountInternalDao.list();
		for(Map<String,Object> accountInternal:list){
			String accountNo=MapUtils.getString(accountInternal, "account_no");
			if(accountNo==null){
				log.error("internal accountNo isnull");
				continue;
			}
			AccountQueryResult account=accountQueryFacade.getAccountInfo(accountNo);
			if(account.isSuccess()){
				AccountVO accountVO=account.getAccountVO();
				accountInternal.put("balance", accountVO.getBalance());
				accountInternal.put("accountNo", accountVO.getAccountNo());
				accountInternal.put("canUserBalance", accountVO.getCanUserBalance());
			}else{
				log.error("account query failed");
			}
		}
		return list;
	}

	
	public Map<String,Map<String,Object>> listMap(){
		Map<String,Map<String,Object>> map = new HashMap<String,Map<String,Object>>();
		List<Map<String, Object>> list = this.list();
		for(Map<String, Object> temp:list){
			map.put(MapUtils.getString(temp, "account_no"), temp);
		}
		return map;
	}
}
